<?php

// Authenticate
require("class.AuthenticationManager.php");
require("class.CommandMenu.php");
if (!$authenticationManager->isLoggedIn() || !$authenticationManager->hasClearance(CLEARANCE_ADMINISTRATOR)) 
{
    Header("Location: login.php?redirect=$_SERVER[PHP_SELF]&clearanceRequired=Administrator");
    exit;
}

// Connect to database.
$dbh = dbConnect();

//define the command menu
include("timesheet_menu.inc");

//use 'No Client' if client id is 0
if ($client_id == 0)
{
    $client_id = getFirstClient();
}
// Calculate the previous month.
setReportDate($year, $month, $day, $next_week, $prev_week, $next_month, $prev_month, $time, $time_middle_month);
    
// Change the date-format for internationalization...
if ($mode == "all") $mode = "monthly";
if ($mode == "weekly") {  
  $query = "SELECT $TIMES_TABLE.proj_id, " .
        "$TIMES_TABLE.task_id, ".
        "sec_to_time(unix_timestamp(end_time) - unix_timestamp(start_time)) as diff_time, ".
        "(unix_timestamp(end_time) - unix_timestamp(start_time)) as diff, " .
        "$PROJECT_TABLE.title, " .
        "$TASK_TABLE.name, ".
        "date_format(start_time, '%Y/%m/%d') as start_date, " .
        "trans_num, ".
		"$USER_TABLE.username, " .
        "$USER_TABLE.first_name, " . 
        "$USER_TABLE.last_name, " . 
        "$TIMES_TABLE.log_message " . 
       "FROM $USER_TABLE, $TIMES_TABLE, $PROJECT_TABLE, $TASK_TABLE ".
       "WHERE $PROJECT_TABLE.client_id='$client_id' AND " .
       "$TIMES_TABLE.uid=$USER_TABLE.username AND " .
       "end_time > 0 AND " . 
       "start_time >= '$year-$month-$day' AND ".
        "$PROJECT_TABLE.proj_id = $TIMES_TABLE.proj_id AND " .
        "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND ".
        "end_time < '".date('Y-m-d 00:00:00',$next_week)."' " .
        "ORDER BY proj_id, task_id, start_time";
} else {
  $query = "select $TIMES_TABLE.proj_id, " .
        "$TIMES_TABLE.task_id, ".
        "sec_to_time(unix_timestamp(end_time) - unix_timestamp(start_time)) as diff_time, ".
        "(unix_timestamp(end_time) - unix_timestamp(start_time)) as diff, " .
        "$PROJECT_TABLE.title, " .
        "$TASK_TABLE.name, ".
        "date_format(start_time, '%Y/%m/%d') as start_date, " .
        "trans_num, ".
		"$USER_TABLE.username, ". 
        "$USER_TABLE.first_name, " .
        "$USER_TABLE.last_name, " .
        "$TIMES_TABLE.log_message " . 
       "FROM $USER_TABLE, $TIMES_TABLE, $PROJECT_TABLE, $TASK_TABLE ".
       "WHERE $PROJECT_TABLE.client_id='$client_id' AND " .
       "$TIMES_TABLE.uid=$USER_TABLE.username AND " .
       "end_time > 0 AND start_time >= '$year-$month-1' AND ".
        "$PROJECT_TABLE.proj_id = $TIMES_TABLE.proj_id AND " .
        "$TASK_TABLE.task_id = $TIMES_TABLE.task_id AND ".
        "end_time < '".date('Y-m-1 00:00:00',$next_month)."' " .
        "ORDER BY proj_id, task_id, start_time";
}
//run the query    
list($qh,$num) = dbQuery($query);
  
//define working variables  
$last_proj_id     = -1;
$last_task_id     = -1;
$total_time       =  0;
$grand_total_time =  0;

?>
<html>
    <head>
        <title>Timesheet.php Report: Days for a specific client</title>
        <?php include ("header.inc"); ?>
    </head>
    <body <?php include ("body.inc"); ?> >
    <?php include ("banner.inc"); ?>
    
    <form action="admin_report_specific_client.php" method="get">
        <input type="hidden" name="month" value="<?php echo $month; ?>">
        <input type="hidden" name="year"  value="<?php echo $year; ?>">
        
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="100%" class="face_padding_cell">
                    <table width="100%" border="0">
                        <tr>
                            <td align="left" nowrap>
                                <table width="100%" height="100%" border="0" cellpadding="1" cellspacing="2">
                                    <tr>
                                        <td align="right" width="0" class="outer_table_heading">Client:</td>
                                        <td align="left" width="100%">
                                            <?php client_select_droplist($client_id, false); ?>
                                        </td>
                                    </tr>
                                </table>
                            </td>                                
                            <td align="center" nowrap class="outer_table_heading">
                                <?php echo date('F Y',mktime(0,0,0,$month,1,$year)) ?>
                            </td>                        
                            <td align="right" nowrap>
                                <?php printPrevNext($time, $next_week, $prev_week, $next_month, $prev_month, $time_middle_month,"client_id=$client_id", $mode); ?>
                            </td>
                        </tr>
                    </table>
                    <table width="100%" align="center" border="0" cellpadding="0" cellspacing="0" class="outer_table">
                        <tr>
                            <td>      
                                <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table_body">
<?php 
    if ($num == 0) {
        print "  <tr>\n";
        print "      <td align=\"center\">\n";
        print "          <i><br>No days recorded.<br><br></i>\n";
        print "      </td>\n";
        print "  </tr>\n";
    }
    else {
        while ($data = dbResult($qh)) {
		    // Project Tiltle:
            // New project, so print out last project total time and start a new table cell.
            if ($last_proj_id != $data["proj_id"]) 
            {
                $last_proj_id = $data["proj_id"];
                if ($grand_total_time) 
                {
                    $formatted_time = $total_time / 60 / 60 / 8;
                    print "<tr><td colspan=\"5\" align=\"right\" class=\"calendar_totals_line_weekly_right\">" .
                            "Total: <span class=\"calendar_total_value_weekly\">$formatted_time days</span></td></tr>\n";
                }
                $current_project_title = stripslashes($data["title"]);      
                print "<tr><td valign=\"top\" colspan=\"5\" class=\"calendar_cell_disabled_right\">" .
                        "<a href=\"javascript:void(0)\" onclick=\"javascript:window.open('proj_info.php?proj_id=$data[proj_id]','Transaction Info','location=0,directories=no,status=no,scrollbar=yes,menubar=no,resizable=1,width=500,height=200')\">$current_project_title</a></td></tr>\n";
                $total_time = 0;
            }
            // print "<tr><td align=\"right\" class=\"calendar_cell_middle\">\n";
			
			// Taek name:
            print "<tr>\n\t<td valign=\"top\" align=\"right\" width=\"40%\" class=\"calendar_cell_right\">\n\t";
            if ($last_task_id != $data["task_id"]) 
	        {
                $last_task_id = $data["task_id"];
                $current_task_name = stripslashes($data["name"]);
                print "<a href=\"javascript:void(0)\" onclick=\"javascript:window.open('task_info.php?task_id=$data[task_id]','Transaction Info','location=0,directories=no,status=no,scrollbar=yes,menubar=no,resizable=1,width=500,height=200')\">$current_task_name</a>&nbsp;\n";
            }
			
			
            print "&nbsp;</td>\n\t";
			
			// Username:
			print "<td valign=\"top\" align=\"left\" width=\"14%\" class=\"calendar_cell_right\"> $data[first_name] $data[last_name] &nbsp;&nbsp;</td>\n\t";
	        print "</td>\n\t";
			
			// Start Date:
			print "<td valign=\"top\" align=\"left\" width=\"6%\" class=\"calendar_cell_right\">$data[start_date] &nbsp;&nbsp;</td>\n\t";
	        print "</td>\n\t"; 
	    
	        // Log message:
	        print "<td valign=\"top\" align=\"left\" class=\"calendar_cell_right\">";
            if ($data['log_message']) 
	        {
                print stripslashes($data['log_message']);
            } 
	        else 
	        {
                print "&nbsp;";  
            }
	        print "</td>\n\t";
	        
	        // Duration:
            print "<td valign=\"top\" align=\"right\" width=\"%\" class=\"calendar_cell_right\">";
	        $duration = $data['diff'] / 60 / 60 / 8;
	        print "$duration days &nbsp;&nbsp;";
	        
	        
	        print "\t</td>\n</tr>\n&nbsp;&nbsp;\n\t</td>\n</tr>\n";
              
            $total_time       += $data["diff"];
            $grand_total_time += $data["diff"];
        }
        
        if ($total_time) 
        {
            $formatted_time = $total_time / 60 / 60 / 8;
            print "<tr><td colspan=\"5\" align=\"right\" class=\"calendar_totals_line_weekly_right\">" .
                    "Total: <span class=\"calendar_total_value_weekly\">$formatted_time days</span></td></tr>";
        }
        $formatted_time = $grand_total_time / 60 / 60 / 8;
    }

?>
                                                    </tr>
                                                </td>
                                            </table>
                                        </td>
                                    </tr>
                                    <?php 
                                      if ($num > 0)  {
                                    ?>
                                    <tr>
                                        <td>
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table_bottom_panel">
                                                <tr>
                                                    <td align="right" class="calendar_totals_line_monthly">
                                                        <?php
                                                          if ($mode == "weekly")
                                                            print "Weekly";
                                                          else
                                                            print "Monthly";
                                                        ?>            
                                                        total:
                                                        <span class="calendar_total_value_monthly"><?php echo $formatted_time; echo ' days';?></span>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                    <?php
                                    }
                                    ?>
                    </table>
          
        
                </td>
            </tr>
        </table>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
	        <tr>
	        	<td align="center" nowrap>
	        	   <a href="admin_report_specific_client_download_xls.php?month=<?php print $month; ?>&year=<?php print $year; ?>&query=<?php print $query; ?>&mode=monthly">
				       <input name="downloadButton" type="button" id="downloadButton" value="Download" onClick="DownloadAdminReportClient()"> </input> 
				   </a>
				   <br><br>
	        	</td>
	        </tr>
        </table>
    </form>
    <?php include ("footer.inc");?>
    </BODY>
</HTML>

